package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.ApplayLizhiActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.ClockBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditApplyTitleBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimTwoSectionEditBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.RichTextResult;
import com.eqdd.yiqidian.ui.select.SelectRichTextActivity;
import com.eqdd.yiqidian.ui.select.SelectSingleStaffActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class ApplySickLeaveActivity extends SlimBaseMultiEditActivity {
    private ApplayLizhiActivityCustom dataBinding;

    private String msg;
    @ToSave
    String date;
    @ToSave
    ArrayList<ClockBean> clockBeans;
    @ToSave
    String checkMan;
    @ToSave
    RichTextResult richTextResult;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_applay_lizhi);
        initTopTitleBar(View.VISIBLE, "消除迟到早退申请");
        initTopRightText("提交", v -> {
            if (checkParam()) {
                PostRequest<HttpResult> params = OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CREATE_SICK_LEAVE)
                        .params("userGuid", user.getGuid())
                        .params("choseDate", date)
                        .params("ids", getId())
                        .params("reason", richTextResult.getContent())
                        .params("witness", checkMan==null?"":checkMan);
                List<LocalMedia> selectMedia = richTextResult.getSelectMedia();
                for (int i = 0; i < selectMedia.size(); i++) {
                    params.params("file", new File(selectMedia.get(i).isCompressed() ? selectMedia.get(i).getCompressPath() : selectMedia.get(i).getPath()));
                }
                params.execute(new DialogCallBack<HttpResult>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            isSave=false;
                            clearData();
                            setResult(Config.SUCCESS);
                            finish();
                        }
                    }
                });

            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private String getId() {
        StringBuilder stringBuilder = new StringBuilder();
        for (ClockBean clockBean : clockBeans) {
            stringBuilder.append(";").append(clockBean.getId());
        }
        return stringBuilder.toString().replaceFirst(";", "");
    }

    private boolean checkParam() {
        if (TextUtils.isEmpty(setParam(0))) {
            msg = "请选择时段";
            return false;
        }
        if (TextUtils.isEmpty(setParam(1))) {
            msg = "请输入原因";
            return false;
        }
        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (p) {
            case 0:
                startActivityForResult(SickLeaveListActivity.class, null, Config.SICK_LEAVE);
                break;
            case 1:
                Bundle bundle = new Bundle();
                bundle.putSerializable(Config.RICH_TEXT_RESULT, richTextResult);
                bundle.putString(Config.TITLE, "原因");
                startActivityForResult(SelectRichTextActivity.class, bundle, Config.SELECT_RICH_TEXT);
                break;
            case 2:
                startActivityForResult(SelectSingleStaffActivity.class, null, Config.SELECT_SINGLE_STAFF);
                break;

        }
    }

    @Override
    protected void setEditBeans() {
        datas.add(new SlimEditEnterBean("迟到早退时间点", "", 0, true, "请选择"));
        datas.add(new SlimEditEnterBean("原因", "", 1, true, "请输入"));
        datas.add(new SlimEditEnterBean("证明人", "", 2, true, "请选择"));
        datas.add(new SlimEditHintBean("审批人", "", 3));
    }


    @Override
    protected void loadNetData() {

        OkGo.<HttpResult<String>>post(HttpConfig.BASE_URL + HttpConfig.GET_LEADER)
                .params("userGuid", user.getGuid())
                .params("companyId", user.getCompanyId())
                .execute(new DialogCallBack<HttpResult<String>>(ApplySickLeaveActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<String>> response) {
                        HttpResult<String> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.getData().get(3).setContent(httpResult.getItems());
                            slimAdapterEx.notifyItemChanged(3);
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Config.SICK_LEAVE && resultCode == Config.SUCCESS) {
            date = data.getStringExtra("date");
            clockBeans = data.getParcelableArrayListExtra("list");
            StringBuilder stringBuilder = new StringBuilder();
            for (ClockBean clockBean : clockBeans) {
                stringBuilder.append(clockBean.getCreateTime() + ",");
            }
            String substring = stringBuilder.substring(0, stringBuilder.length() - 1);
            datas.get(0).setContent(date + "  " + substring);
            slimAdapterEx.notifyItemChanged(0);
        } else if (requestCode == Config.SELECT_SINGLE_STAFF && resultCode == Config.INTENT_CONTENT) {
            String content = data.getStringExtra("content");
            checkMan = data.getStringExtra("guid");
            datas.get(2).setContent(content);
            slimAdapterEx.notifyItemChanged(2);
        } else if (resultCode == Config.SUCCESS && requestCode == Config.SELECT_RICH_TEXT) {
            richTextResult = (RichTextResult) data.getSerializableExtra(Config.RICH_TEXT_RESULT);
            datas.get(1).setContent(richTextResult.getContent());
            slimAdapterEx.notifyItemChanged(1);
        }
    }
}
